package com.quanquan.user.message;

import com.quanquan.user.service.MarketingRulesService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

/**
 * 功能描述:处理新用户是否有福利
 *
 * @ClassName: MqSignInContinuousReceiver
 * @Author: xiaoqianh丶
 * @Date: 2019-04-05 23:02
 * @Version: V1.0
 */
@Slf4j
@Component
public class MqNewUserRewardReceiver {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MarketingRulesService marketingRulesService;


    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "newUserReward",
                    durable = "true"),
            exchange = @Exchange(value = "newUser.exchange",
                    type = "topic"),
            ignoreDeclarationExceptions = "true",
            key = "newUser.*")})
    @RabbitHandler
    public void checkSignContinuous(Message message, Channel channel) throws IOException {
        //会员id
        String userId = Objects.requireNonNull(message.getHeaders().get("userId")).toString();
        System.out.println("userId"+userId);
        System.out.println("开始处理");
        marketingRulesService.checkoutWelfareRule(userId);
        System.out.println("结束");
        //手工ack
        long deliveryTag = Long.parseLong(Objects.requireNonNull(message.getHeaders().get(AmqpHeaders.DELIVERY_TAG)).toString());
        channel.basicAck(deliveryTag, false);




    }
}